<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Http\Resources\Estate\PlaformOwnerResource\PlaformOwnerCollection;
use App\Models\AdminEstate;
use App\Models\AlarmPlan;
use App\Models\ChargingScheme;
use App\Models\ElectricCommunity;
use App\Models\ElectricCommunityArea;
use App\Models\ElectricCommunityElement;
use App\Models\ElectricCommunityFloor;
use App\Models\ElectricCommunityRoom;
use App\Models\ElectricCommunityTower;
use App\Models\ElectricPlatformOwner;
use App\Models\PlatformOwner;
use Illuminate\Http\Request;

class PlatformOwnerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data=$request->query();
        $where=[];
        if (isset($data['truename']))
        {
            $where[] = ['truename', 'like' ,"%{$data['truename']}%"];
        }
        if (isset($data['mobile']))
        {
            $where[] = ['mobile', 'like' ,"%{$data['mobile']}%"];
        }
        if (isset($data['community_id']))
        {
            $where['community'] = $data['community_id'];
        }
        $list = PlatformOwner::query()
            ->with(['community_data','community_areas_data'])
            ->where($where)
            ->orderBy('id', 'desc')
            ->paginate($request->per_page ?? 30);
//        return $this->success($list);
        return $this->success(new PlaformOwnerCollection($list));
    }

    public function estatePlaformownerInof($id = null,PlatformOwner $PlatformOwner)
    {
        if ($post=$_POST){

            if (isset($post['id']))
            {
                if(isset($post['last_payment_time']))
                {
                    $post['last_payment_time']=strtotime ($post['last_payment_time']);
                }
                $query = $PlatformOwner
                    ->where('id' , $post['id'])
                    ->update($post);
            }else{
                $query = $PlatformOwner->insert($post);
            }

            return $this->success($query);
        }else {
            $community=$this->community($id);
            $query = $PlatformOwner::query()
                ->where('id', $id)
                ->orderBy('id', 'desc')
                ->first();
            $query['last_payment_time'] = date('Y-m-d', $query['last_payment_time']);
            $data['community']=$community;
            $data['data']=$query;
            return $this->success($data);
        }

    }
    public function getCommunity()
    {
        $communityList = ElectricCommunity::query()
            ->where(['status' => 1])
            ->get();

        return $this->success($communityList);
    }
    public function community($id)
    {
        $address = ElectricPlatformOwner::query()
            ->where('id', $id)
            ->select('community','community_area','tower_id','element_id','floor_id','room_id')
            ->first();
        $where['c_id'] = $address['community'];
        $whereArea['area_id'] = $address['community_area'];
        $whereTower['tower_id'] = $address['tower_id'];
        $whereElement['element_id'] = $address['element_id'];
        $whereFloor['floor_id'] = $address['floor_id'];

        $communityList = ElectricCommunity::query()
            ->where(['status' => 1])
            ->get();

        $communityAreaList = ElectricCommunityArea::query()
            ->where(['status' => 1])->where($where)
            ->get();

        $communityTowerList = ElectricCommunityTower::query()
            ->where(['status' => 1])->where($whereArea)
            ->get();

        $communityElementList = ElectricCommunityElement::query()
            ->where(['status' => 1])->where($whereTower)
            ->get();

        $communityFloorList = ElectricCommunityFloor::query()
            ->where(['status' => 1])->where($whereElement)
            ->get();

        $communityRoomList = ElectricCommunityRoom::query()
            ->where(['status' => 1])->where($whereFloor)
            ->get();

        return ['communityList'=>$communityList, 'communityAreaList'=>$communityAreaList,'communityTowerList'=>$communityTowerList,'communityElementList'=>$communityElementList,'communityFloorList'=>$communityFloorList,'communityRoomList'=>$communityRoomList,'address'=>$address];
    }


    public function communityAreaList($id)
    {
        $communityAreaList = ElectricCommunityArea::query()
            ->where(['status' => 1 ,'c_id'=>$id])
            ->get();
        return $this->success($communityAreaList);
    }
    public function communityTowerList($id)
    {
        $communityTowerList = ElectricCommunityTower::query()
            ->where(['status' => 1 , 'area_id' =>$id])
            ->get();
        return $this->success($communityTowerList);
    }
    public function communityElementList($id)
    {
        $communityElementList = ElectricCommunityElement::query()
            ->where(['status' => 1, 'tower_id' =>$id])
            ->get();
        return $this->success($communityElementList);
    }
    public function communityFloorList($id)
    {
        $communityFloorList = ElectricCommunityFloor::query()
            ->where(['status' => 1, 'element_id' =>$id])
            ->get();
        return $this->success($communityFloorList);
    }
    public function communityRoomList($id)
    {
        $communityRoomList = ElectricCommunityRoom::query()
            ->where(['status' => 1, 'floor_id' =>$id])
            ->get();
        return $this->success($communityRoomList);
    }
    public function getchargingScheme()
    {
        $AlarmPlan[] = ChargingScheme::query()
            ->where(['status' => 1, 'fee_type' =>3])
            ->select('id','plan_title')
            ->get();
        $AlarmPlan[] = AlarmPlan::query()
            ->where(['status' => 1, 'alarm_type' =>1])
            ->select('id','plan_title')
            ->get();
        $AlarmPlan[] = AlarmPlan::query()
            ->where(['status' => 1, 'alarm_type' =>2])
            ->select('id','plan_title')
            ->get();
        $AlarmPlan[] = AlarmPlan::query()
            ->where(['status' => 1, 'alarm_type' =>3])
            ->select('id','plan_title')
            ->get();
        return $this->success($AlarmPlan);
    }


    public function editPlaformownerStatus(PlatformOwner $PlatformOwner)
    {

        $data=$_POST;
        if ($data){;
            $info = $PlatformOwner
                ->where('id', $data['id'])
                ->first();
            if ($info[$data['type']] == 0)
            {
                $query = $PlatformOwner
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>1]);

            }else if ($info[$data['type']] == 1){

                $query = $PlatformOwner
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>0]);
            }
            return $this->success($query);
        }
        return $this->error();

    }
    public function editPlatformOwnerDel($id)
    {

        $ids=explode(',',$id);
        $query = PlatformOwner::query()
            ->whereIn('id' , $ids)
            ->delete();
        return $this->success($query);

    }







    /**
     * Display the specified resource.
     *
     * @param AlarmPlan $meter_model
     * @param int $id
     * @return array|Response
     */
    public function show (PlatformOwner $meter_model, $id)
    {
        $info = $meter_model->find($id);
        return $this->success($info);
    }
}
